import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { ConfigProvider } from 'antd-v4';
import classNames from 'classnames';
import { useContext } from 'react';
import { clearMenuItem } from '../../utils/utils';
import { defaultRenderCollapsedButton, defaultRenderLogo, defaultRenderLogoAndTitle } from '../SiderMenu/SiderMenu';
import TopNavHeader from '../TopNavHeader';
import './index.less';

var renderLogo = function renderLogo(menuHeaderRender, logoDom) {
  if (menuHeaderRender === false) {
    return null;
  }

  if (menuHeaderRender) {
    return menuHeaderRender(logoDom, null);
  }

  return logoDom;
};

var GlobalHeader = function GlobalHeader(props) {
  var isMobile = props.isMobile,
      logo = props.logo,
      collapsed = props.collapsed,
      onCollapse = props.onCollapse,
      _props$collapsedButto = props.collapsedButtonRender,
      collapsedButtonRender = _props$collapsedButto === void 0 ? defaultRenderCollapsedButton : _props$collapsedButto,
      rightContentRender = props.rightContentRender,
      menuHeaderRender = props.menuHeaderRender,
      onMenuHeaderClick = props.onMenuHeaderClick,
      propClassName = props.className,
      style = props.style,
      layout = props.layout,
      children = props.children,
      _props$headerTheme = props.headerTheme,
      headerTheme = _props$headerTheme === void 0 ? 'dark' : _props$headerTheme,
      splitMenus = props.splitMenus,
      menuData = props.menuData,
      prefixCls = props.prefixCls;

  var _useContext = useContext(ConfigProvider.ConfigContext),
      direction = _useContext.direction;

  var baseClassName = "".concat(prefixCls, "-global-header");
  var className = classNames(propClassName, baseClassName, _defineProperty({}, "".concat(baseClassName, "-layout-").concat(layout), layout && headerTheme === 'dark'));

  if (layout === 'mix' && !isMobile && splitMenus) {
    var noChildrenMenuData = (menuData || []).map(function (item) {
      return _objectSpread(_objectSpread({}, item), {}, {
        children: undefined,
        routes: undefined
      });
    });
    var clearMenuData = clearMenuItem(noChildrenMenuData);
    return _jsx(TopNavHeader, _objectSpread(_objectSpread({
      mode: "horizontal"
    }, props), {}, {
      splitMenus: false,
      menuData: clearMenuData,
      theme: headerTheme
    }));
  }

  var logoClassNames = classNames("".concat(baseClassName, "-logo"), _defineProperty({}, "".concat(baseClassName, "-logo-rtl"), direction === 'rtl'));

  var logoDom = _jsx("span", {
    className: logoClassNames,
    children: _jsx("a", {
      children: defaultRenderLogo(logo)
    })
  }, "logo");

  return _jsxs("div", {
    className: className,
    style: _objectSpread({}, style),
    children: [isMobile && renderLogo(menuHeaderRender, logoDom), isMobile && collapsedButtonRender && _jsx("span", {
      className: "".concat(baseClassName, "-collapsed-button"),
      onClick: function onClick() {
        if (onCollapse) {
          onCollapse(!collapsed);
        }
      },
      children: collapsedButtonRender(collapsed)
    }), layout === 'mix' && !isMobile && _jsx(_Fragment, {
      children: _jsx("div", {
        className: logoClassNames,
        onClick: onMenuHeaderClick,
        children: defaultRenderLogoAndTitle(_objectSpread(_objectSpread({}, props), {}, {
          collapsed: false
        }), 'headerTitleRender')
      })
    }), _jsx("div", {
      style: {
        flex: 1
      },
      children: children
    }), rightContentRender && rightContentRender(props)]
  });
};

export default GlobalHeader;